﻿using Communergy.Powermeter;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Communergy.Common;
using System;
using CommunergyLogicTests.Helpers;
using System.Net;
//using CommunergyLogicTests.Helpers;

namespace CommunergyLogicTests
{
    
    
    /// <summary>
    ///This is a test class for GooglePowermeterExportCommandTest and is intended
    ///to contain all GooglePowermeterExportCommandTest Unit Tests
    ///</summary>
    [TestClass()]
    public class GooglePowermeterExportCommandTest
    {







        /// <summary>
        ///A test for GetPostUrl
        ///</summary>
        [TestMethod()]
        public void GetPostUrlTest()
        {
            string meterId = string.Empty; // TODO: Initialize to an appropriate value
            IRepositoryFactory cacheRepository = null; // TODO: Initialize to an appropriate value
            TimeSpan ageOfRecords = new TimeSpan(); // TODO: Initialize to an appropriate value
            PowermeterData powerMeterData = PowermeterHelper.GetTestPowermeterData();
           

            GooglePowermeterExportCommand target = new GooglePowermeterExportCommand(meterId, ageOfRecords, powerMeterData); // TODO: Initialize to an appropriate value
            target.LocalCacheRepository = cacheRepository;
            string expected = "https://www.google.com/powermeter/feeds/user/07214542406431945487/07214542406431945487/variable/Communergy.CurrentCost128.Home.d2/durMeasurement";
            
            string actual;
            actual = target.GetPostUrl();
            Assert.AreEqual(expected, actual);
            
        }



        /// <summary>
        ///A test for ExportData
        ///</summary>
        [TestMethod()]
        public void GooglePowermeter_ExportData_Hardcoded_XML()
        {
            string meterId = string.Empty; // TODO: Initialize to an appropriate value
            IRepositoryFactory cacheRepository = null; // TODO: Initialize to an appropriate value
            TimeSpan ageOfRecords = new TimeSpan(); // TODO: Initialize to an appropriate value
            PowermeterData powerMeterData = PowermeterHelper.GetTestPowermeterData();
            GooglePowermeterExportCommand target = new GooglePowermeterExportCommand(meterId, ageOfRecords, powerMeterData); // TODO: Initialize to an appropriate value
            target.LocalCacheRepository = cacheRepository;
            string exportXml = PowermeterHelper.POST_XML;
            string url = target.GetPostUrl();

            var response = GooglePowermeterExportCommand.PostXml(url, exportXml, powerMeterData.AuthToken);
            Assert.AreEqual(HttpStatusCode.Created, response);
        } 
        
        [TestMethod()]
        public void GooglePowermeter_ExportData_Generated_XML()
        {
            string meterId = string.Empty; // TODO: Initialize to an appropriate value
            IRepositoryFactory cacheRepository = null; // TODO: Initialize to an appropriate value
            TimeSpan ageOfRecords = new TimeSpan(); // TODO: Initialize to an appropriate value
            PowermeterData powerMeterData = PowermeterHelper.GetTestPowermeterData();
            GooglePowermeterExportCommand target = new GooglePowermeterExportCommand(meterId,  ageOfRecords, powerMeterData); // TODO: Initialize to an appropriate value
            target.LocalCacheRepository = cacheRepository;

            HistoricalEnergyReading exportData = PowermeterHelper.RecentHistoricalReading();
            string exportXml = GooglePowermeterExportCommand.GetExportXml(exportData);

            string url = target.GetPostUrl();

            var response = GooglePowermeterExportCommand.PostXml(url, exportXml, powerMeterData.AuthToken);
            Assert.AreEqual(HttpStatusCode.Created, response);
        }
    }
}
